package com.swpu.cbg.cnshop.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.swpu.cbg.cnshop.R;
import com.swpu.cbg.cnshop.bean.ShoppingCart;
import com.swpu.cbg.cnshop.bean.Ware;
import com.swpu.cbg.cnshop.utils.CartProvider;

import java.util.List;

/**
 * Created by chenboge on 16/2/5.
 */


public class HotAdapter extends RecyclerView.Adapter<HotAdapter.ViewHolder> {


    private Context mContext;
    private List<Ware> mDatas;
    private onItemClickListener onItemClickListener;
    private OnItemButtonClickListener onItemButtonClickListener;
    private CartProvider mProvider;
    private int resID;


    public HotAdapter(Context mContext, List<Ware> wares) {
        this.mContext = mContext;
        this.mDatas = wares;
        this.resID = R.layout.template_hot_recycleview;

    }

    public void setOnItemClickListener(onItemClickListener itemClickListener) {
        this.onItemClickListener = itemClickListener;
    }


    public void setOnItemButtonClickListener(OnItemButtonClickListener onItemButtonClickListener) {
        this.onItemButtonClickListener = onItemButtonClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = null;
        mView = LayoutInflater.from(mContext).inflate(resID, parent, false);
        ViewHolder mHOlder = new ViewHolder(mView);
        return mHOlder;
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Ware ware = mDatas.get(position);
        holder.tv_content.setText(ware.getName());
        holder.tv_price.setText(+ware.getPrice() + "");
        Picasso.with(mContext).load(ware.getImgUrl()).into(holder.iv_left_image);
        holder.mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mProvider = new CartProvider(mContext);
                ShoppingCart cart = new ShoppingCart();
                cart.setName(mDatas.get(position).getName());
                cart.setPrice(mDatas.get(position).getPrice());
                cart.setImgUrl(mDatas.get(position).getImgUrl());
                cart.setId(mDatas.get(position).getId());
                mProvider.put(cart);
                Toast.makeText(mContext, "添加" + mDatas.get(position).getName() + "成功", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView tv_content;
        TextView tv_price;
        Button mButton;
        ImageView iv_left_image;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_left_image = (ImageView) itemView.findViewById(R.id.id_hot_img);
            tv_content = (TextView) itemView.findViewById(R.id.id_hot_content);
            tv_price = (TextView) itemView.findViewById(R.id.id_hot_price);
            mButton = (Button) itemView.findViewById(R.id.id_hot_btn);
        }

        @Override
        public void onClick(View v) {
            Ware ware = mDatas.get(getLayoutPosition());
            switch (v.getId()) {
                case R.id.imgview_big:
                    if (onItemClickListener != null) {
                        onItemClickListener.onClick(v, ware);
                    }
                    break;

                case R.id.id_hot_btn:
                    onItemButtonClickListener.OnClick(v,ware);
                    break;
            }

        }
    }


    public interface onItemClickListener {

        void onClick(View view, Ware ware);
    }

    public void AddData(List<Ware> data) {
        AddData(0, data);
    }

    public List<Ware> getDatas() {
        return mDatas;
    }

    public void AddData(int position, List<Ware> data) {
        if (data != null && data.size() > 0) {
            mDatas.addAll(data);
            notifyItemRangeChanged(position, mDatas.size());
        }
    }

    public void ClearData() {
        mDatas.clear();
        notifyItemMoved(0, mDatas.size());
    }

    public void resetLayOut(int resid) {
        this.resID = resid;

    }

    public interface OnItemButtonClickListener {
        void OnClick(View view, Ware ware);
    }
}
